/*

REPORTS ON THE RESULTS FROM THE ASI DATA 

*Tables and Figures in the Main Paper
Figure 2
Table 1
Table 3

*Appendix Tables and Figures
Table A2
Table A3
Table A4
Table A5
Table A7
*/




use "${data}/APcontractlabor_final1.dta", clear
tabulate year, generate (yr_)

keep if statusunit==1

generate treat00 = treat*yr_1
generate treat01 = treat*yr_2
generate treat02 = treat*yr_3
generate treat03 = treat*yr_4
generate treat04 = treat*yr_5
generate treat05 = treat*yr_6
generate treat06 = treat*yr_7
generate treat07 = treat*yr_8

generate zero = 0
label var treat00 "1999-2000"
label var treat01 "2000-2001"
label var treat02 "2001-2002"
label var treat03 "2002-2003"
label var treat04 "2003-2004"
label var treat05 "2004-2005"
label var treat06 "2005-2006"
label var treat07 "2006-2007"
label var zero "2002-2003"

gen regularwages_total = regularsalaries/regular_totmandays
gen lrwages_total = ln(regularwages_total)
gen realregularwages = regularwages_total *(wpiop2011/wpiop)
gen lrealregularwages = ln( realregularwages) 
gen regularwagespaid_total = regularsalaries/regularmandayspaid
gen lrwagespaid_total = ln(regularwagespaid_total)
gen realregularwagespaid_total = regularwagespaid_total *(wpiop2011/wpiop)
gen lrealregularwagespaid_total = ln(realregularwagespaid_total)

gen price = egen_exfactoryvalop1/egen_qtysold1
gen lprice = ln(price)

gen lcapital = lplantmachinery_grossclosing  
egen stateyear = group(state year)
egen stateind4digit = group(state ind4digit)

*cd "/Users/rchaurey/Dropbox/APcontract_reducedform/output/"
*cd "C:\Users\shouv\Dropbox (Personal)\APcontract_reducedform\output"
label variable lcw "log [contract workers]"
label variable cwratio "contract workers ratio"
label variable lrw "log [regular workers]"
label variable lcm_man "log [contract worker person-days]"
label variable lrm_man "log [regular worker person-days]"
label variable cratio_man "contract persondays ratio" 
label variable posttreat "Post X Treat"

gen contracttoregular_workers = contract_workers/regular_workers
gen contracttoregular_mandays = contract_mandays/regularmandays 
gen logcontracttoregular_workers = ln(contracttoregular_workers+1)
gen logcontracttoregular_mandays = ln(contracttoregular_mandays+1)

gen cw = exp(lncw)
gen w = exp(lnw)
gen rw = exp(lnrw)
gen cwratio_new = cw/w

drop lncw_w lnrw_w 

foreach v of varlist  lcm_man cratio_man cwratio lcw lcm_total lcm_nonman contract_workers contract_mandays contract_totmandays cratio_total cratio_nonman workers regular_workers regular_totmandays regularmandays regularwages_total allrevenue lrw lw lrwages_total lrm_man lrm_nonman lwm_total lrm_total lnrw lncw lnw cwratio_new cw rw w {
winsor2 `v', cuts(1 99)
}

gen age = year - yearinitialprod
replace age = . if year< yearinitialprod
replace age = . if yearinitialprod==0
replace age = . if yearinitialprod<1900

gen ind2digit = int(ind3digit/10)

global fe "factory year ind2digit##i.year state##c.year"
global cl "state"



cap drop temp_sample
qui reghdfe cwratio_new_w posttreat c.age##c.age ///
			if year!=2007 [pw=weight], cluster($cl) absorb($fe)
gen temp_sample = e(sample)



***********
* Table 1 
***********
replace contract_mandays_w = contract_mandays_w/1000
replace regularmandays_w = regularmandays_w/1000

est clear
label var cw_w "Contract Workers"
label var rw_w "Regular Workers"
label var w_w "Total Workers"
label var contract_mandays_w "Contract Worker-days ('000)"
label var regularmandays_w "Regular Worker-days ('000)"
label var cwratio_new_w "Frac. Contract Workers"
label var cratio_man_w "Frac. Contract Worker-days"
estpost summ cw_w rw_w w_w ///
			 contract_mandays_w regularmandays_w /// 
			 cwratio_new_w cratio_man_w ///
			 if  temp_sample==1 & treat==0 & post==0 & year!=2007
eststo c1
estpost summ cw_w rw_w w_w ///
			 contract_mandays_w regularmandays_w /// 
			 cwratio_new_w cratio_man_w ///
			 if  temp_sample==1 & treat==1 & post==0 & year!=2007
eststo c2

esttab c2 c1 using "${output}/asi/Table1.tex", replace  ///
cell((count(label(N)) mean(fmt(%9.2f) label(Mean)) sd(fmt(%9.2f) label(SD)))) /// 
mtitles("\underline{Andhra Pradesh}" "\underline{All Other States}") ///
collabels("N" "Mean" "SD") ///
 label not noobs nonotes booktabs nonotes 


*******************************
* Figure 2 and TABLE A2
*******************************

**Event study graphs 

est clear 
local i=1
foreach v of varlist lncw_w lcm_total_w lnrw_w lrm_total_w cwratio_new_w cratio_total_w {
	
	qui reghdfe `v' treat01 treat02 zero treat04 treat05 treat06 ///
			if year!=2007 & temp_sample == 1 [pw=weight], cluster($cl) absorb($fe)
	estimates store model`v' 
	eststo c`i'
	qui estadd local factoryfe "Yes"
	qui estadd local yearfe "Yes"
	qui estadd local indyearfe "Yes"
	qui estadd local statetrend "Yes"
	qui summ `v' if treat == 0 & post == 0 & year!=2007
	qui estadd scalar mean = r(mean)
	qui estadd scalar rsq = e(r2)
	qui test treat01 = treat02 = 0
	qui estadd scalar pval = r(p)
	local i=`i'+1
	di "Done with `v'"
}

# delimit ;
esttab  c1 c2 c3 c4 c5 c6 using "${output}/asi/TableA2.tex", replace
keep(treat01 treat02 treat04 treat05 treat06)
cells(b(fmt(%5.3f) star) se(fmt(%5.3f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Log Contract" "Log Regular" "Frac. Contract", pattern(1 0 1 0 1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Workers" "Worker-Days" "Workers" "Worker-Days" "Workers" "Worker-Days")
stats(pval rsq N, labels("p-val:" "R2" "N")
fmt(2 2 0)) collabels(none)   label booktabs nonotes
;
#delimit cr 

		
coefplot (c1, recast(connected) lcolor(black) mcolor(black) msymbol(square) ///
		  ciopts(recast(rarea) fcolor(gs12) lcolor(gs12))) ///
		 (c2, mcolor(red) msymbol(diamond) ciopts(recast(rcap) lcolor(red))) ///
					, vertical yline(0, lcolor(gs10) lp(dash))  ///
					keep(treat* zero) omitted levels (90) ///
					xtitle(" ") bgcolor(white) ylabel(, format(%02.1f)) ///
					graphregion(color(white)) lcolor(emidblue) ///
					legend(pos(6) rows(1) label(2 "Log Contract Workers") label(4 "Log Contract Worker-Days"))
graph export "${output}/asi/Figure2a.png", as(png) name("Graph") replace

coefplot (c3, recast(connected) lcolor(black) mcolor(black) msymbol(square) ///
		  ciopts(recast(rarea) fcolor(gs12) lcolor(gs12))) ///
		 (c4, mcolor(red) msymbol(diamond) ciopts(recast(rcap) lcolor(red))) ///
					, vertical yline(0, lcolor(gs10) lp(dash))  ///
					keep(treat* zero) omitted levels (90) ///
					xtitle(" ") bgcolor(white) ylabel(, format(%02.1f)) ///
					graphregion(color(white)) lcolor(emidblue) ///
					legend(pos(6) rows(1) label(2 "Log Regular Workers") label(4 "Log Regular Worker-Days"))
graph export "${output}/asi/Figure2b.png", as(png) name("Graph") replace

coefplot (c5, recast(connected) lcolor(black) mcolor(black) msymbol(square) ///
		  ciopts(recast(rarea) fcolor(gs12) lcolor(gs12))) ///
		 (c6, mcolor(red) msymbol(diamond) ciopts(recast(rcap) lcolor(red))) ///
					, vertical yline(0, lcolor(gs10) lp(dash))  ///
					keep(treat* zero) omitted levels (90) ///
					xtitle(" ") bgcolor(white) ylabel(, format(%02.1f)) ///
					graphregion(color(white)) lcolor(emidblue) ///
					legend(pos(6) rows(1) label(2 "Frac. Contract Workers") label(4 "Frac. Contract Worker-Days"))
graph export "${output}/asi/Figure2c.png", as(png) name("Graph") replace



	
***************************************
* 3. Table 3
***************************************

est clear 
local i=1
foreach v of varlist lncw_w lcm_total_w lnrw_w lrm_total_w cwratio_new_w cratio_total_w lnw_w lwm_total_w {
	
	qui reghdfe `v' posttreat c.age##c.age ///
			if year!=2007 & temp_sample == 1 [pw=weight], cluster($cl) absorb($fe)
	eststo c`i'
	qui estadd local factoryfe "Yes"
	qui estadd local yearfe "Yes"
	qui estadd local indyearfe "Yes"
	qui estadd local statetrend "Yes"
	qui estadd scalar rsq = e(r2)
	qui summ `v' if treat == 0 & post == 0 & year!=2007 & temp_sample == 1 
	qui estadd scalar mean = r(mean)
	local i=`i'+1
	di "Done with `v'"
}

# delimit ;
esttab  c1 c5 c2 c6 using "${output}/asi/Table3.tex", replace
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Workers" "Worker-Days", pattern(1 0 1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Log Contract" "Frac. Contract" "Log Contract" "Frac. Contract")
stats(rsq, labels("R2")fmt(2)) 
posthead("\midrule \multicolumn{4}{l}{\textit{\textbf{Panel A: Impact on Usage of Contract Workers}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes
;
#delimit cr	


# delimit ;
esttab  c3 c7 c4 c8 using "${output}/asi/Table3.tex", append
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel B: Impact on Payroll Workers and Firm Size}}} \\ \midrule")
mtitles("Log Contract" "Frac. Contract" "Log Contract" "Frac. Contract")
collabels(, none) nonumbers label booktabs nonotes mlabels(none) 
stats(rsq N, labels("R2" "N")fmt(2 0)) 
;
#delimit cr	




***********APPENDIX*******************






***************************************
* Table A3: Core vs Non Core Results
***************************************
est clear 
local i=1
foreach v of varlist lcm_man lrm_man cratio_man lcm_nonman lrm_nonman cratio_nonman   {
	qui reghdfe `v' posttreat if year!=2007 [pw=weight] ///
			, cluster(${cl}) absorb(${fe})
	eststo c`i'
	qui estadd local factoryfe "Yes"
	qui estadd local yearfe "Yes"
	qui estadd local indyearfe "Yes"
	qui estadd local statetrend "Yes"
	qui estadd scalar rsq = e(r2)
	local i=`i'+1
}



# delimit ;
esttab  c1 c2 c3 using "${output}/asi/TableA3.tex", replace
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mtitles("Log Contract Workers" "Log Regular Workers" "Frac. Contract Workers")
stats(rsq, labels("R2")fmt(2)) 
posthead("\midrule \multicolumn{4}{l}{\textit{\textbf{Panel A: Impact on Core Activities}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes
;
#delimit cr	


# delimit ;
esttab  c4 c5 c6 using "${output}/asi/TableA3.tex", append
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel B: Impact on Non-Core Activities}}} \\ \midrule")
mtitles(none)
collabels(, none) mlabels(none) nonumbers label booktabs nonotes 
stats(rsq N, labels("R2" "N")fmt(2 0)) 
;
#delimit cr	









***************************************
* Table A4. ONLY WITH NEIGHBORING STATES
***************************************
cap drop temp_sample
qui reghdfe cwratio_new_w posttreat c.age##c.age ///
			if year!=2007 & neighbor == 1 [pw=weight], cluster($cl) absorb($fe)
gen temp_sample = e(sample)

est clear 
local i=1
foreach v of varlist lncw_w lcm_total_w lnrw_w lrm_total_w cwratio_new_w cratio_total_w lnw_w lwm_total_w {
	*lcw_w lcm_man_w cratio_man_w cwratio_w
	qui reghdfe `v' posttreat c.age##c.age ///
			if year!=2007 & temp_sample == 1  & neighbor == 1 [pw=weight], cluster($cl) absorb($fe)
	eststo c`i'
	qui estadd local factoryfe "Yes"
	qui estadd local yearfe "Yes"
	qui estadd local indyearfe "Yes"
	qui estadd local statetrend "Yes"
	qui estadd scalar rsq = e(r2)
	qui summ `v' if treat == 0 & post == 0 & year!=2007 & temp_sample == 1  & neighbor == 1 
	qui estadd scalar mean = r(mean)
	local i=`i'+1
	di "Done with `v'"
}

# delimit ;
esttab  c1 c5 c2 c6 using "${output}/asi/TableA4.tex", replace
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Workers" "Worker-Days", pattern(1 0 1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Log Contract" "Frac. Contract" "Log Contract" "Frac. Contract")
stats(rsq, labels("R2")fmt(2)) 
posthead("\midrule \multicolumn{4}{l}{\textit{\textbf{Panel A: Impact on Usage of Contract Workers}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes
;
#delimit cr	


# delimit ;
esttab  c3 c7 c4 c8 using "${output}/asi/TableA4.tex", append
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel B: Impact on Payroll Workers and Firm Size}}} \\ \midrule")
mtitles("Log Regular" "Log Total" "Log Regular" "Log Total")
collabels(, none) nonumbers label booktabs nonotes mlabels(none) 
stats(rsq N, labels("R2" "N")fmt(2 0)) 
;
#delimit cr	

 




***************************************
* Table A5. WITHOUT NEIGHBORING STATES
***************************************
cap drop temp_sample
qui reghdfe cwratio_new_w posttreat c.age##c.age ///
			if year!=2007 & (neighbor == 0 | treat == 1) [pw=weight], cluster($cl) absorb($fe)
gen temp_sample = e(sample)

est clear 
local i=1
foreach v of varlist lncw_w lcm_total_w lnrw_w lrm_total_w cwratio_new_w cratio_total_w lnw_w lwm_total_w {
	*lcw_w lcm_man_w cratio_man_w cwratio_w
	qui reghdfe `v' posttreat c.age##c.age ///
			if year!=2007 & temp_sample == 1  & (neighbor == 0 | treat == 1)  [pw=weight], cluster($cl) absorb($fe)
	eststo c`i'
	qui estadd local factoryfe "Yes"
	qui estadd local yearfe "Yes"
	qui estadd local indyearfe "Yes"
	qui estadd local statetrend "Yes"
	qui estadd scalar rsq = e(r2)
	qui summ `v' if treat == 0 & post == 0 & year!=2007 & temp_sample == 1  & neighbor == 1 
	qui estadd scalar mean = r(mean)
	local i=`i'+1
	di "Done with `v'"
}

# delimit ;
esttab  c1 c5 c2 c6 using "${output}/asi/TableA5.tex", replace
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Workers" "Worker-Days", pattern(1 0 1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Log Contract" "Frac. Contract" "Log Contract" "Frac. Contract")
stats(rsq, labels("R2")fmt(2)) 
posthead("\midrule \multicolumn{4}{l}{\textit{\textbf{Panel A: Impact on Usage of Contract Workers}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes 
;
#delimit cr	


# delimit ;
esttab  c3 c7 c4 c8 using "${output}/asi/TableA5.tex", append
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{3}{l}{\textit{\textbf{Panel B: Impact on Payroll Workers and Firm Size}}} \\ \midrule")
mtitles("Log Regular" "Log Total" "Log Regular" "Log Total")
collabels(, none) nonumbers label booktabs nonotes mlabels(none) 
stats(rsq N, labels("R2" "N")fmt(2 0)) 
;
#delimit cr	













*************************************************
* A3. ALTERNATE DEFINITIONS OF FIXED EFFECTS - Table A7
*************************************************
est clear 
local i=1
foreach v of varlist lncw_w lcm_total_w lnrw_w lrm_total_w cwratio_new_w cratio_total_w {
	*lcw_w lcm_man_w cratio_man_w cwratio_w

	*Without Any FE
	qui reghdfe `v' posttreat c.age##c.age ///
			if year!=2007 [pw=weight], cluster($cl) absorb(state##c.year year)
	eststo basic`i'
	qui estadd local factoryfe "No"
	qui estadd local stateind "No"
	qui estadd local indyearfe "No"
	qui estadd scalar rsq = e(r2)
	qui summ `v' if treat == 0 & post == 0 & year!=2007 
	qui estadd scalar mean = r(mean)



	*Industry-Year FE
	qui reghdfe `v' posttreat c.age##c.age ///
			if year!=2007 [pw=weight], cluster($cl) absorb(state##c.year ind2digit##year year)
	eststo ife`i'
	qui estadd local factoryfe "No"
	qui estadd local stateind "No"
	qui estadd local indyearfe "Yes"
	qui estadd scalar rsq = e(r2)
	qui summ `v' if treat == 0 & post == 0 & year!=2007 
	qui estadd scalar mean = r(mean)

	*State-Industry FE
	qui reghdfe `v' posttreat c.age##c.age ///
			if year!=2007 [pw=weight], cluster($cl) absorb(state##c.year state##ind2digit year)
	eststo sfe`i'
	qui estadd local factoryfe "No"
	qui estadd local stateind "Yes"
	qui estadd local indyearfe "No"
	qui estadd scalar rsq = e(r2)
	qui summ `v' if treat == 0 & post == 0 & year!=2007 
	qui estadd scalar mean = r(mean)


	*State-Industry and Industry-Year FE
	qui reghdfe `v' posttreat c.age##c.age ///
			if year!=2007 [pw=weight], cluster($cl) absorb(state##c.year ind2digit##year state##ind2digit year)
	eststo isfe`i'
	qui estadd local factoryfe "No"
	qui estadd local stateind "Yes"
	qui estadd local indyearfe "Yes"
	qui estadd scalar rsq = e(r2)
	qui summ `v' if treat == 0 & post == 0 & year!=2007 
	qui estadd scalar mean = r(mean)

	*Baseline
	qui reghdfe `v' posttreat c.age##c.age ///
			if year!=2007 [pw=weight], cluster($cl) absorb(${fe})
	eststo baseline`i'
	qui estadd local factoryfe "No"
	qui estadd local stateind "No"
	qui estadd local indyearfe "No"
	qui estadd scalar rsq = e(r2)
	qui summ `v' if treat == 0 & post == 0 & year!=2007 
	qui estadd scalar mean = r(mean)

	local i = `i' + 1
	di "Done with `v'"
}

# delimit ;
esttab  basic* using "${output}/asi/TableA7.tex", replace
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
mgroups("Log Contract" "Log Regular" "Fraction of Contract", pattern(1 0 1 0 1 0)
span prefix(\multicolumn{@span}{c}{) suffix(}) erepeat(\cmidrule(lr){@span})) 
mtitles("Workers" "Worker-Days" "Workers" "Worker-Days" "Workers" "Worker-Days" "Workers" "Worker-Days")
stats(rsq, labels("R2")fmt(2)) 
posthead("\midrule \multicolumn{6}{l}{\textit{\textbf{Panel A: Without Additional Fixed Effects}}} \\ \midrule")
postfoot("")
collabels(none) label booktabs nonotes 
;
#delimit cr	


# delimit ;
esttab  ife* using "${output}/asi/TableA7.tex", append
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{6}{l}{\textit{\textbf{Panel B: 2-digit Industry-Year Fixed Effects}}} \\ \midrule")
mtitles(none)
postfoot("")
stats(rsq, labels("R2")fmt(2)) 
collabels(, none) nonumbers label booktabs nonotes mlabels(none) 
;
#delimit cr	

# delimit ;
esttab  sfe* using "${output}/asi/TableA7.tex", append
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{6}{l}{\textit{\textbf{Panel C: State-2-digit Industry Fixed Effects}}} \\ \midrule")
mtitles(none)
postfoot("")
stats(rsq, labels("R2")fmt(2)) 
collabels(, none) nonumbers label booktabs nonotes mlabels(none) 
;
#delimit cr	


# delimit ;
esttab  isfe* using "${output}/asi/TableA7.tex", append
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{6}{l}{\textit{\textbf{Panel D: 2-digit Industry-Year and State-2-digit Industry Fixed Effects}}} \\ \midrule")
mtitles(none)
postfoot("")
stats(rsq, labels("R2")fmt(2)) 
collabels(, none) nonumbers label booktabs nonotes mlabels(none) 
;
#delimit cr	

# delimit ;
esttab  baseline* using "${output}/asi/TableA7.tex", append
keep(posttreat)
cells(b(fmt(%5.2f) star) se(fmt(%5.2f) par))
starlevels(* .10 ** .05 *** .01) 
prehead("")
posthead("\midrule \multicolumn{6}{l}{\textit{\textbf{Panel E: Preferred Specification in the Main Paper}}} \\ \midrule")
mtitles(none)
collabels(, none) nonumbers label booktabs nonotes mlabels(none) 
stats(rsq N, labels("R2" "N")fmt(2 0)) 
;
#delimit cr	

